package cn.edu.scau.cmi.liangzijian.composite.consistent;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.liangzijian.composite.safe.SafeOrganization;

public class ConsistentTeam implements ConsistentOrganization {
	private List<ConsistentOrganization> memberList = new ArrayList<ConsistentOrganization>();
	
	private String name;
	
	private ConsistentOrganization parent;
	
	public ConsistentTeam(String name) {
		this.name = name;
	}
	
	@Override
	public boolean add(ConsistentOrganization p) {
		String name = p.getName();
		
		for(ConsistentOrganization so : memberList) {
			if(name.equals(so.getName())) {
				if((p instanceof ConsistentPerson && so instanceof ConsistentPerson)||
						p instanceof ConsistentTeam && so instanceof ConsistentTeam) {
					return false;
				} 
			}
		}
		p.setParent(this);
		memberList.add(p);
		return true;
	}
		
	@Override
	public boolean remove(String name) {
		for(ConsistentOrganization so : memberList) {
			if(name.equals(so.getName())) {
				memberList.remove(so);
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public boolean remove(ConsistentOrganization p) {
		return memberList.remove(p);
	}
	
	@Override
	public ConsistentOrganization getOrganization(String name) {
		for(ConsistentOrganization so : memberList) {
			if(name.equals(so.getName())) {
				return so;
			}
		}
		
		return null;
	}
	
	@Override
	public List<ConsistentOrganization> getMemberList() {
		return memberList;
	}
	
	@Override
	public void say() {
		for(ConsistentOrganization person : memberList) {
			person.say();
		}
	}

	@Override
	public String getName() {
		return name;
	}
	
	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setParent(ConsistentOrganization parent) {
		this.parent = parent;		
	}

	@Override
	public ConsistentOrganization getParent() {
		return parent;
	}
}
